package sbc.group1.lab1.mozartspaces;

import java.net.URI;
import java.net.URL;

import org.mozartspaces.core.AsyncCapi;
import org.mozartspaces.core.Capi;
import org.mozartspaces.core.DefaultMzsCore;
import org.mozartspaces.core.MzsConstants;
import org.mozartspaces.core.MzsCoreException;
import org.mozartspaces.core.TransactionReference;
import org.mozartspaces.core.config.CommonsXmlConfiguration;

class SpaceClient extends Space {
   
    private URI spaceUri;
    private AsyncCapi acapi;
    
    public void init(URL configUrl, URI spaceURI) throws MzsCoreException {
        this.spaceUri = spaceURI;
        core = DefaultMzsCore.newInstance(CommonsXmlConfiguration.loadFrom(configUrl));
        capi = new Capi(core);
        acapi = new AsyncCapi(core);
        
        assembledCarContainer =   capi.lookupContainer(ASSEMBLED_CAR_CONTAINER_NAME, 
                spaceURI, 
                MzsConstants.Container.UNBOUNDED, (TransactionReference)null);
        completedCarContainer =   capi.lookupContainer(COMPLETED_CAR_CONTAINER_NAME, 
                spaceURI, 
                MzsConstants.Container.UNBOUNDED, (TransactionReference)null);
        completenessTestedContainer =   capi.lookupContainer(COMPLETENESS_TESTED_CONTAINER_NAME, 
                spaceURI, 
                MzsConstants.Container.UNBOUNDED, (TransactionReference)null);
        defectsTestedContainer =   capi.lookupContainer(DEFECTS_TESTED_CONTAINER_NAME, 
                spaceURI, 
                MzsConstants.Container.UNBOUNDED, (TransactionReference)null);
        testedCarContainer =  capi.lookupContainer(TESTED_CAR_CONTAINER_NAME, 
                spaceURI, 
                MzsConstants.Container.UNBOUNDED, (TransactionReference)null);
        finishedCarContainer =  capi.lookupContainer(FINISHED_CAR_CONTAINER_NAME, 
                spaceURI, 
                MzsConstants.Container.UNBOUNDED, (TransactionReference)null);
        defectCarContainer =  capi.lookupContainer(DEFECT_CAR_CONTAINER_NAME, 
                spaceURI, 
                MzsConstants.Container.UNBOUNDED, (TransactionReference)null);
        carBodyContainer = capi.lookupContainer(CAR_BODY_CONTAINER_NAME, 
                spaceURI, 
                MzsConstants.Container.UNBOUNDED, (TransactionReference)null);
        engineContainer = capi.lookupContainer(ENGINE_CONTAINER_NAME, 
                spaceURI, 
                MzsConstants.Container.UNBOUNDED, (TransactionReference)null);
        wheelContainer = capi.lookupContainer(WHEEL_CONTAINER_NAME, 
                spaceURI, 
                MzsConstants.Container.UNBOUNDED, (TransactionReference)null);
        orderContainer = capi.lookupContainer(ORDER_CONTAINER_NAME,
                spaceURI,
                MzsConstants.Container.UNBOUNDED, (TransactionReference) null);
        orderIndexContainer = capi.lookupContainer(ORDER_INDEX_CONTAINER_NAME, 
                spaceURI,
                MzsConstants.Container.UNBOUNDED, (TransactionReference)null);
        
        orderJobsReissueContainer = capi.lookupContainer(ORDER_JOB_REISSUE_CONTAINER_NAME,
                spaceURI,
                MzsConstants.Container.UNBOUNDED, (TransactionReference) null);
    }
    
    public URI getSpaceUri() {
        return spaceUri;
    }
    
    public AsyncCapi getAcapi() {
        return acapi;
    }
}
